package com.iss.airsys.controller;

import com.iss.airsys.entity.Userinfo;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.model.UserinfoWithUserWithRole;
import com.iss.airsys.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("userinfo/")
public class UserInfoController {
    private boolean flag = false;
    @Autowired
    UserInfoService service;
    @Autowired
    MyResponseData data;

    @RequestMapping("selectAll")
    public List<UserinfoWithUserWithRole> selectAll() {
        List<UserinfoWithUserWithRole> list = service.selectAll();
        System.out.println("============="+list);
        return list;
    }


    @RequestMapping("deleteUserInfo")
    public MyResponseData deleteUserInfo(String[] uInfo_names) {
        try {
            if (uInfo_names[0] != null) {
                flag = service.deletePeople(uInfo_names);
                if (flag) {
                    data.setErrorCode(0);
                    data.setStatus("删除成功!");
                    data.setObject(flag);
                } else {
                    data.setErrorCode(-1);
                    data.setStatus("删除失败!");
                }
            } else {
                data.setErrorCode(-1);
                data.setStatus("请进行勾选!");
            }
        } catch (Exception e) {
            System.out.println("我是NullPointerException");
            data.setErrorCode(-1);
            data.setStatus("请进行勾选!");

        }

        return data;

    }

    @RequestMapping("insertUserinfo")
    public MyResponseData insertUserinfo(Userinfo userinfo){
        //先看主键是否重复
         Userinfo userinfo1=service.selectUserinfobyName(userinfo.getUInfo_name());
        try {
            if (userinfo1==null) {
                flag = service.insertPeople(userinfo);
                if (flag) {
                    data.setErrorCode(0);
                    data.setStatus("插入成功");
                    data.setObject(userinfo);
                } else {
                    data.setErrorCode(-1);
                    data.setStatus("插入失败");
                }
            } else {
                data.setErrorCode(-2);
                data.setStatus("编号已存在");
            }
        }catch (Exception e){
            data.setErrorCode(-3);
            data.setStatus("请完善未填写的信息");
        }

        return data;
    }

    //更新roomfree
    @RequestMapping("updatetUserinfo")
    public MyResponseData updatetUserinfo(Userinfo userinfo){
        flag=service.updatePeople(userinfo);
        if (flag){
            data.setErrorCode(0);
            data.setStatus("更新成功!");
        }else{
            data.setErrorCode(-1);
            data.setStatus("更新失败!");
        }
        return data;
    }

    @RequestMapping("selectUserinfobyName")
    public MyResponseData selectUserinfobyName(String uInfo_name){
        Userinfo userinfo=service.selectUserinfobyName(uInfo_name);
        if (flag){
            data.setErrorCode(0);
            data.setObject(userinfo);
            data.setStatus("成功!");
        }else{
            data.setErrorCode(-1);
            data.setStatus("失败!");
        }
        return data;
    }



}